package org.example.seata.common.security.config.auth.grant;

import lombok.Getter;
import org.springframework.security.authentication.AbstractAuthenticationToken;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.UserDetails;

/**
 * @author hzq
 * @date 2021/12/6 10:46
 */
public class CustomAuthenticationToken extends AbstractAuthenticationToken {

    /**
     * 处理器类型
     */
    @Getter
    private String handlerType;

    /**
     * 标识
     */
    private Object principal;

    /**
     * 凭证
     */
    private Object credentials;

    public CustomAuthenticationToken(String handlerType, Object principal, Object credentials) {
        super(AuthorityUtils.NO_AUTHORITIES);
        this.handlerType = handlerType;
        this.principal = principal;
        this.credentials = credentials;
    }

    public CustomAuthenticationToken(UserDetails userDetails) {
        super(userDetails.getAuthorities());
        this.principal = userDetails;
        super.setAuthenticated(true);
    }

    @Override
    public Object getPrincipal() {
        return this.principal;
    }

    @Override
    public Object getCredentials() {
        return this.credentials;
    }
}
